package com.engc.common.utils;

import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 信息推送公共类，将信息发送给推送平台，由推送平台统一发送
 * 
 * @author Administrator
 * 
 */

public class MessagePush {
	private static Log log = LogFactory.getLog(MessagePush.class);// 日志记录器
	private String ip = new ReadProperties("config/messagepush.properties").getValue("ServerIp");
	private String port = new ReadProperties("config/messagepush.properties").getValue("ServerPort");
	
	
	public static final int TypeOfSendSMS=1;
	public static final int TypeOfSendEmail = 2;
	public static final int TypeOfSendPCDiskTop = 3;
	public static final int TypeOfSendPhotoDiskTop = 4;

	private static MessagePush me;
	public static MessagePush getInstance(){
		if(me==null){
			me = new MessagePush();
		}
		return me;
	}
	private MessagePush(){
		
	}
    
	

	/**
	 * 发送推送信息
	 * @param message
	 * @param user
	 * @param type
	 */
	public void sendForPush(String sendcode,String title,String msgInfo,String msgUrl,String endTime,ArrayList<String> receiverMsg,int type)  {
		try {
			Socket sk = new Socket(ip, Integer.parseInt(port));
			OutputStream out = sk.getOutputStream();
			if(out!=null){
				if (receiverMsg.size() > 0) {
					String sendmessage = "SOCKET;"+type+";"+sendcode+";"+title+";"+msgInfo+";"+msgUrl;
					if(endTime!=null){
						sendmessage=sendmessage+";"+endTime;
					}
					for (int i = 0; i < receiverMsg.size(); i++) {
						sendmessage=sendmessage+";"+receiverMsg.get(i);
					}
					log.info(sendmessage);
					//发送信息
					out.write(sendmessage.getBytes("UTF-8"));
					out.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
